package com.atguigu.gulimall.test_todolist.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import net.sf.jsqlparser.expression.DateTimeLiteralExpression.DateTime;

import com.atguigu.gulimall.test_todolist.entity.ItemEntity;
import com.atguigu.gulimall.test_todolist.service.ItemService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-10-03 09:39:24
 */
@RestController
@RequestMapping("test_todolist/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("test_todolist:item:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = itemService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("test_todolist:item:info")
    public R info(@PathVariable("id") Long id) {
        ItemEntity item = itemService.getById(id);

        return R.ok().put("item", item);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("test_todolist:item:save")
    public R save(@RequestBody ItemEntity item) {
        item.setCreateTime(new Date().getTime());
        itemService.save(item);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("test_todolist:item:update")
    public R update(@RequestBody ItemEntity item) {
        if (item.getStatus().equals(1)) {
            item.setFinishTime(new Date().getTime());
        }else{
            item.setFinishTime(null);
        }
        itemService.updateById(item);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("test_todolist:item:delete")
    public R delete(@RequestBody Long[] ids) {
        itemService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
